package com.jzit168.module_home

import android.content.Context
import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.RecyclerView
import com.jzit168.library_utils.LogUtil
import kotlin.math.roundToInt

/**
 *@Desc:
 *@Author: Android Developer
 *@Date: 2020/10/23
 *
 **/
class MyDividerItemDecoration(context: Context, orientation: Int) :
    RecyclerView.ItemDecoration() {

    private var mDivider: Drawable? = null

    //方向
    private var mOrientation = 0

    private val mBounds = Rect()

    var drawable: Drawable?
        get() = mDivider
        set(drawable) {
            requireNotNull(drawable) { "Drawable cannot be null." }
            mDivider = drawable
        }

    private fun setOrientation(orientation: Int) {
        require(!(orientation != HORIZONTAL && orientation != VERTICAL)) { "未知的orientation,只能使用HORIZONTAL或者VERTICAL" }
        mOrientation = orientation
    }


    companion object {

        //水平方向
        const val HORIZONTAL = LinearLayout.HORIZONTAL

        //垂直方向
        const val VERTICAL = LinearLayout.VERTICAL

        private val attrs = intArrayOf(android.R.attr.listDivider)

        private const val TAG = "MyDividerItemDecoration"

    }


    init {
        try {
            val typeArray = context.obtainStyledAttributes(attrs)
            mDivider = typeArray.getDrawable(0)
            if (mDivider == null) {
                LogUtil.e(
                    "$TAG---@android:attr/listDivider was not set in the theme used for this " +
                            "DividerItemDecoration. Please set that attribute all call setDrawable()"
                )
            }
            typeArray.recycle()
            setOrientation(orientation)
        } catch (e: Exception) {
            LogUtil.e(e)
        }
    }


    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        if (parent.layoutManager == null || mDivider == null) {
            return
        }
        if (mOrientation == VERTICAL) {
            //在垂直方向上绘制item分割
            drawableVertical(c, parent)
        } else {
            //在水平方向上绘制item分割
            drawHorizontal(c, parent)
        }
    }

    private fun drawableVertical(canvas: Canvas, parent: RecyclerView) {
        canvas.save()
        val left: Int
        val right: Int

        //如果parent有设置padding内边距
        //四个方位的值需要减去内边距才是item的位置
        //ps: 左顶右下的初始方向是x方向向右为正,y方向向下为正
        if (parent.clipToPadding) {
            LogUtil.w("RecyclerView有添加内边距:${parent.paddingLeft}")
            left = parent.paddingLeft
            val top: Int = parent.paddingTop
            right = parent.width - parent.paddingRight
            val bottom: Int = parent.height - parent.paddingBottom
            //绘制矩形的位置
            canvas.clipRect(left, top, right, bottom)
        } else {
            left = 0
            right = parent.width
        }

        //获取parent的item数量
        val childCount = parent.childCount
        LogUtil.d("$TAG---childCount的数量:$childCount")
        for (i in 0 until childCount) {
            //获取每一个Item
            val child = parent.getChildAt(i)
            //获取视图边界
            parent.getDecoratedBoundsWithMargins(child, mBounds)
            val bottom = mBounds.bottom + child.translationY.roundToInt()
            val top = bottom - mDivider!!.intrinsicHeight
            mDivider!!.setBounds(left, top, right, bottom)
            mDivider!!.draw(canvas)
        }
        canvas.restore()
    }

    private fun drawHorizontal(canvas: Canvas, parent: RecyclerView) {
        canvas.save()
        val top: Int
        val bottom: Int
        if (parent.clipToPadding) {
            top = parent.paddingTop
            bottom = parent.height - parent.paddingBottom
            canvas.clipRect(
                parent.paddingLeft, top,
                parent.width - parent.paddingRight, bottom
            )
        } else {
            top = 0
            bottom = parent.height
        }
        val childCount = parent.childCount
        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            parent.layoutManager!!.getDecoratedBoundsWithMargins(child, mBounds)
            val right = mBounds.right + child.translationX.roundToInt()
            val left = right - mDivider!!.intrinsicWidth
            mDivider!!.setBounds(left, top, right, bottom)
            mDivider!!.draw(canvas)
        }
        canvas.restore()
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        if (mDivider == null) {
            outRect[0, 0, 0] = 0
            return
        }
        if (mOrientation == VERTICAL) {
            outRect[0, 0, 0] = mDivider!!.intrinsicHeight
        } else {
            outRect[0, 0, mDivider!!.intrinsicWidth] = 0
        }
    }

}